import sys,subprocess,glob
from multiprocessing import Pool
import time

date = time.strftime("%Y%m%d",time.localtime())
binRootDir = '/data/softwares/mngs_scripts/auto/autoPathoSeqPy'
Rscript = '/data/softwares/R4.0.4/R-4.0.4_install/bin/Rscript'

def autoNT(indir,infile):
    tmp = infile.strip().split('/')[-1].split('.')
    runid  = infile.strip().split('/')[-2]
    name = f'{tmp[0]}.{tmp[1]}.{tmp[2]}'
    script = f'''mkdir -p /data/pub/today/.auto/guangzhou/{runid} && mkdir -p /data/pub/today/.auto/guangzhou/{runid}/output
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/
    export PATH=$JAVA_HOME/bin:$PATH
    export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/jre/lib/amd64/server/:$LD_LIBRARY_PATH
    export PYTHONPATH=/data/POC/shijun_mngs/mngs_report/merge/autoPathoSeqPy/src/

    /data/POC/shijun_mngs/mngs_report/merge/autoPathoSeqPy/venv/bin/python3 {binRootDir}/src/main.py \
    -d NT \
    -l GZ \
    -c {infile} \
    -j {binRootDir}/tag/CF_Jaccard.KK.txt \
    -o /data/pub/today/.auto/guangzhou/{runid}/output/{name}.autoRes.NT.xlsx \
    -t {binRootDir}/tag/PathoSeq_Microbe.Py_20210721.xlsx \
    -s {binRootDir}/tag/PathoSeq_SampleType.txt \
    -a /data/softwares/mngs_scripts/auto/autoPathoSeqWeb/batch-0713 \
    >py.GZ_NT.log 2>&1

    {Rscript} /data/POC/runmngs/autoPathoSeqPy/concat_xlsx.pyAddCooksD.R /data/pub/today/.auto/guangzhou/{runid}/output /data/pub/today/guangzhou/{runid}/kkbwa/GZ.autoTest.{date}.xlsx'''
    #print(script)
    subprocess.run(script, shell=True, stderr=subprocess.PIPE)

def autoKK(indir,infile):
    tmp = infile.strip().split('/')[-1].split('.')
    runid  = infile.strip().split('/')[-2]
    name = f'{tmp[0]}.{tmp[1]}.{tmp[2]}'
    script = f'''mkdir -p /data/pub/today/.auto/guangzhou/{runid} && mkdir -p /data/pub/today/.auto/guangzhou/{runid}/output
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/
    export PATH=$JAVA_HOME/bin:$PATH
    export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/jre/lib/amd64/server/:$LD_LIBRARY_PATH
    export PYTHONPATH=/data/POC/shijun_mngs/mngs_report/merge/autoPathoSeqPy/src/
    
    /data/POC/shijun_mngs/mngs_report/merge/autoPathoSeqPy/venv/bin/python3 {binRootDir}/src/main.py \
    -d KK \
    -l GZ \
    -c {infile} \
    -j {binRootDir}/tag/CF_Jaccard.KK.txt \
    -o /data/pub/today/.auto/guangzhou/{runid}/output/{name}.autoRes.KK.xlsx \
    -t {binRootDir}/tag/PathoSeq_Microbe.Py_20210721.xlsx \
    -s {binRootDir}/tag/PathoSeq_SampleType.txt \
    -a /data/softwares/mngs_scripts/auto/autoPathoSeqWeb/batch-0713 \
    >py.GZ_KK.log 2>&1

    {Rscript} /data/POC/runmngs/autoPathoSeqPy/concat_xlsx.pyAddCooksD.R /data/pub/today/.auto/guangzhou/{runid}/output /data/pub/today/guangzhou/{runid}/kkbwa/GZ.autoTest.{date}.xlsx'''
    subprocess.run(script, shell=True, stderr=subprocess.PIPE)

if __name__ == "__main__":
    p = Pool(30)
    inputdir = sys.argv[1]
    dbName = sys.argv[2]
    files = glob.glob(f'{inputdir}/*.xlsx')
    for f in files:
        if dbName == 'NT':
            if f.find(str(dbName)) > -1:
                p.apply_async(autoNT,(inputdir,f))
        elif dbName == 'KK':
            if f.find(str(dbName)) > -1:
                p.apply_async(autoKK,(inputdir,f))
    p.close()
    p.join()
